package com.service.api;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.Cookie;
import org.jboss.netty.handler.codec.http.CookieDecoder;
import org.jboss.netty.handler.codec.http.CookieEncoder;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpChunk;
import org.jboss.netty.handler.codec.http.HttpChunkTrailer;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMessage;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.handler.codec.http.QueryStringDecoder;
import org.jboss.netty.util.CharsetUtil;

public class HttpRequestHandler extends SimpleChannelUpstreamHandler {
	private HttpRequest request;
	private boolean readingChunks;
	private final StringBuilder buf = new StringBuilder();

	@SuppressWarnings("rawtypes")
	public void messageReceived(
			ChannelHandlerContext paramChannelHandlerContext,
			MessageEvent paramMessageEvent) throws Exception {
		Object localObject1;
		Object localObject2;
		Object localObject3;
		Object localObject4;
		Object localObject5;
		String str1;
		if (!(this.readingChunks)) {
			localObject1 = this.request = (HttpRequest) paramMessageEvent
					.getMessage();
			if (HttpHeaders.is100ContinueExpected((HttpMessage) localObject1))
				send100Continue(paramMessageEvent);
			this.buf.setLength(0);
			this.buf.append("WELCOME TO THE WILD WILD WEB SERVER\r\n");
			this.buf.append("===================================\r\n");
			this.buf.append("VERSION: "
					+ ((HttpRequest) localObject1).getProtocolVersion()
					+ "\r\n");
			this.buf.append("HOSTNAME: "
					+ HttpHeaders
							.getHost((HttpMessage) localObject1, "unknown")
					+ "\r\n");
			this.buf.append("REQUEST_URI: "
					+ ((HttpRequest) localObject1).getUri() + "\r\n\r\n");
			localObject2 = ((HttpRequest) localObject1).getHeaders().iterator();
			while (((Iterator) localObject2).hasNext()) {
				localObject3 = (Map.Entry) ((Iterator) localObject2).next();
				this.buf.append("HEADER: "
						+ ((String) ((Map.Entry) localObject3).getKey())
						+ " = "
						+ ((String) ((Map.Entry) localObject3).getValue())
						+ "\r\n");
			}
			this.buf.append("\r\n");
			localObject2 = new QueryStringDecoder(
					((HttpRequest) localObject1).getUri());
			localObject3 = ((QueryStringDecoder) localObject2).getParameters();
			if (!(((Map) localObject3).isEmpty())) {
				localObject4 = ((Map) localObject3).entrySet().iterator();
				while (((Iterator) localObject4).hasNext()) {
					localObject5 = (Map.Entry) ((Iterator) localObject4).next();
					str1 = (String) ((Map.Entry) localObject5).getKey();
					List localList = (List) ((Map.Entry) localObject5)
							.getValue();
					Iterator localIterator = localList.iterator();
					while (localIterator.hasNext()) {
						String str2 = (String) localIterator.next();
						this.buf.append("PARAM: " + str1 + " = " + str2
								+ "\r\n");
					}
				}
				this.buf.append("\r\n");
			}
			if (((HttpRequest) localObject1).isChunked()) {
				this.readingChunks = true;
			} else {
				localObject4 = ((HttpRequest) localObject1).getContent();
				if (((ChannelBuffer) localObject4).readable())
					this.buf.append("CONTENT: "
							+ ((ChannelBuffer) localObject4)
									.toString(CharsetUtil.UTF_8) + "\r\n");
				writeResponse(paramMessageEvent);
			}
		} else {
			localObject1 = (HttpChunk) paramMessageEvent.getMessage();
			if (((HttpChunk) localObject1).isLast()) {
				this.readingChunks = false;
				this.buf.append("END OF CONTENT\r\n");
				localObject2 = (HttpChunkTrailer) localObject1;
				if (!(((HttpChunkTrailer) localObject2).getHeaderNames()
						.isEmpty())) {
					this.buf.append("\r\n");
					localObject3 = ((HttpChunkTrailer) localObject2)
							.getHeaderNames().iterator();
					while (((Iterator) localObject3).hasNext()) {
						localObject4 = (String) ((Iterator) localObject3)
								.next();
						localObject5 = ((HttpChunkTrailer) localObject2)
								.getHeaders((String) localObject4).iterator();
						while (((Iterator) localObject5).hasNext()) {
							str1 = (String) ((Iterator) localObject5).next();
							this.buf.append("TRAILING HEADER: "
									+ ((String) localObject4) + " = " + str1
									+ "\r\n");
						}
					}
					this.buf.append("\r\n");
				}
				writeResponse(paramMessageEvent);
			} else {
				this.buf.append("CHUNK: "
						+ ((HttpChunk) localObject1).getContent().toString(
								CharsetUtil.UTF_8) + "\r\n");
			}
		}
	}

	@SuppressWarnings("rawtypes")
	private void writeResponse(MessageEvent paramMessageEvent) {
		boolean bool = HttpHeaders.isKeepAlive(this.request);
		DefaultHttpResponse localDefaultHttpResponse = new DefaultHttpResponse(
				HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
		localDefaultHttpResponse.setContent(ChannelBuffers.copiedBuffer(
				this.buf.toString(), CharsetUtil.UTF_8));
		localDefaultHttpResponse.setHeader("Content-Type",
				"text/plain; charset=UTF-8");
		if (bool)
			localDefaultHttpResponse.setHeader("Content-Length", Integer
					.valueOf(localDefaultHttpResponse.getContent()
							.readableBytes()));
		String str = this.request.getHeader("Cookie");
		if (str != null) {
			Object localObject = new CookieDecoder();
			Set localSet = ((CookieDecoder) localObject).decode(str);
			if (!(localSet.isEmpty())) {
				CookieEncoder localCookieEncoder = new CookieEncoder(true);
				Iterator localIterator = localSet.iterator();
				while (localIterator.hasNext()) {
					Cookie localCookie = (Cookie) localIterator.next();
					localCookieEncoder.addCookie(localCookie);
				}
				localDefaultHttpResponse.addHeader("Set-Cookie",
						localCookieEncoder.encode());
			}
		}
		Object localObject = paramMessageEvent.getChannel().write(
				localDefaultHttpResponse);
		if (bool)
			return;
		((ChannelFuture) localObject).addListener(ChannelFutureListener.CLOSE);
	}

	private void send100Continue(MessageEvent paramMessageEvent) {
		DefaultHttpResponse localDefaultHttpResponse = new DefaultHttpResponse(
				HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE);
		paramMessageEvent.getChannel().write(localDefaultHttpResponse);
	}

	public void exceptionCaught(
			ChannelHandlerContext paramChannelHandlerContext,
			ExceptionEvent paramExceptionEvent) throws Exception {
		paramExceptionEvent.getCause().printStackTrace();
		paramExceptionEvent.getChannel().close();
	}
}